package com.hl.card.award.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hl.card.common.util.ResponseData;
import com.hl.card.common.util.ResponseObject;
import com.hl.card.award.service.IRewardByTrans;
import com.hl.card.award.service.LoginUserService;
import com.hl.card.common.persistence.dao.UserMapper;
import com.hl.card.common.persistence.model.User;
import com.hl.card.common.util.DateUtils;
import com.hl.card.modular.pack.dto.AwardUser;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class LoginUserServiceImpl extends ServiceImpl<UserMapper, User> implements LoginUserService {

    private Logger logger= LoggerFactory.getLogger(LoginUserServiceImpl.class);


    @Autowired
    private IRewardByTrans rewardByTrans;
    @Override
    public Boolean checkRegCode(String regCode) {
        Wrapper<User> warp=new EntityWrapper<User>();
        warp.eq("reg_code",regCode);
        User re = super.selectOne(warp);
        return null!=re;
    }
    @Override
    public User getUserByRegCode(String regCode) {
        Wrapper<User> warp=new EntityWrapper<User>();
        warp.eq("reg_code",regCode);
        User re = super.selectOne(warp);
        return re;
    }

    @Override
    public ResponseObject reward(String awardUser) {
        AwardUser awardUserInfo = JSON.parseObject(awardUser, AwardUser.class);
        String userId= awardUserInfo.getUserId();
        System.out.println("领取新注册会员福利:userId=" + userId);
        if(StringUtils.isBlank(userId)){
            return  null;
        }
        Date regDate= awardUserInfo.getRegDate();
        Boolean isOlder = DateUtils.isOldUser(regDate);
        String awardType;
        if(isOlder){
            awardType="LYHFL";
        }else if(StringUtils.isBlank(awardUserInfo.getRegCode())){
            awardType="ZCHY";
        }else{
            awardType="YQZC";
        }
        rewardByTrans.reward(userId,awardType);


        logger.debug("用户注册奖励类型:"+awardType+"==>"+ awardUser.toString());

        return null;
    }

}
